//
//  FKTabBarVC.m
//  TextOCR
//
//  Created by frank on 2018/12/22.
//  Copyright © 2018 Macbook Pro. All rights reserved.
//

#import "FKTabBarVC.h"
#import "FKNavVC.h"

@interface FKTabBarVC ()<UITabBarControllerDelegate>

@end

@implementation FKTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTabBar) name:@"changeTabBar" object:nil];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupViewControllers];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)changeTabBar
{
    if ([FKCacheTool isLogin]) {
        if ([FKCacheTool isMember]) {
            [self showMemberCenter];
        } else {
            [self hideMemberCenter];
        }
    } else {
        [self hideMemberCenter];
    }
}

- (void)setupViewControllers
{
    [self addChildViewController:[[NSClassFromString(@"FKHomeVC") alloc] init] title:@"首页" imageName:@"tab_home"];
    [self addChildViewController:[[NSClassFromString(@"FKGoodsListVC") alloc] init] title:@"商品分类" imageName:@"tab_mall"];
    if ([FKCacheTool isMember]) {
        [self addChildViewController:[[NSClassFromString(@"FKMemberCenterVC") alloc] init] title:@"会员中心" imageName:@"tab_member"];
    }
    [self addChildViewController:[[NSClassFromString(@"FKFastShareVC") alloc] init] title:@"一键发圈" imageName:@"tab_share"];
    [self addChildViewController:[[NSClassFromString(@"FKMineVC1") alloc] init] title:@"我的" imageName:@"tab_mine"];
}

- (void)showMemberCenter
{
    if (self.childViewControllers.count == 4) {
        [self addChildViewController:[[NSClassFromString(@"FKMemberCenterVC") alloc] init] title:@"会员中心" imageName:@"tab_member"];
        NSMutableArray *array = [NSMutableArray arrayWithArray:self.childViewControllers];
        UIViewController *temp = array.lastObject;
        [array removeLastObject];
        [array insertObject:temp atIndex:2];
        [self setViewControllers:array];
    }
}

- (void)hideMemberCenter
{
    if (self.childViewControllers.count == 5) {
        NSMutableArray *array = [NSMutableArray arrayWithArray:self.childViewControllers];
        [array removeObjectAtIndex:2];
        [self setViewControllers:array];
    }
}

/**
 添加子控制器
 
 @param childVC 子控制器
 @param title 标题
 @param imageName 默认状态图片名称
 */
- (void)addChildViewController:(UIViewController *)childVC title:(NSString *)title imageName:(NSString *)imageName
{
    childVC.title = title;
    
    // 设置子控制器的图片
    childVC.tabBarItem.image = [UIImage imageNamed:imageName];
    NSString *selectedName = [NSString stringWithFormat:@"%@_selected", imageName];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    // 设置文字的样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = fkColor666666;
    [childVC.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    NSMutableDictionary *selectTextAttrs = [NSMutableDictionary dictionary];
    selectTextAttrs[NSForegroundColorAttributeName] = fkMainColor;
    [childVC.tabBarItem setTitleTextAttributes:selectTextAttrs forState:UIControlStateSelected];
    
    // 先给外面传进来的小控制器 包装 一个导航控制器
    FKNavVC *nav = [[FKNavVC alloc] initWithRootViewController:childVC];
    [self addChildViewController:nav];
    
    
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    [self changeTabBar];
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([viewController.tabBarItem.title isEqualToString:@"我的"] ) {
        [FKLoginTool checkLogin:nil];
        return [FKCacheTool isLogin];
    } else if ([viewController.tabBarItem.title isEqualToString:@"一键发圈"]) {
        [FKLoginTool checkLogin:nil];
        return [FKCacheTool isLogin];
    }
    return true;
}


@end
